require "Dialogs"
require "Frisk Character"
Arena.ResizeImmediate(155, 130)
FriskDirection = "R"
AlowFriskMovement(false, false)
Player.SetControlOverride(true)

if Encounter["phase"] > 1 and Encounter["NotUsedItems"] and string.sub(GetAlMightyGlobal("Archivimentazo"), 12, 12) == "0" then
	SetAlMightyGlobal("Archivimentazo", string.sub(GetAlMightyGlobal("Archivimentazo"), 1, 11).."1"..string.sub(GetAlMightyGlobal("Archivimentazo"), 13))
	Encounter["LogroDesbloqueado"] = {Timer = 0, Icon = 12}
elseif Encounter["phase"] == 1 and Encounter["NOHIT"] and string.sub(GetAlMightyGlobal("Archivimentazo"), 16, 16) == "0" then
	SetAlMightyGlobal("Archivimentazo", string.sub(GetAlMightyGlobal("Archivimentazo"), 1, 15).."1"..string.sub(GetAlMightyGlobal("Archivimentazo"), 17))
	Encounter["LogroDesbloqueado"] = {Timer = 0, Icon = 16}
end
timezone = string.gsub(package.loaded.os.date("%Z"), ":00", "")
timezone = string.gsub(timezone, ":30", "")
hour = tonumber(package.loaded.os.date("%H")) + tonumber(timezone)
while hour >= 24 do
	hour = hour - 24
end
while hour < 0 do
	hour = hour + 24
end
day = tonumber(package.loaded.os.date("%d"))
month = tonumber(package.loaded.os.date("%m"))

BlackDefinitive = CreateSprite("Bullets/Bones/Body","BelowBullet")
Corredor = CreateSprite("Overworld/Maps/Corredor","BelowBullet")
CorredorN = CreateSprite("Overworld/Maps/Corredor Noche","BelowBullet")
CorredorN.SetParent(Corredor)
PilarOw = CreateSprite("Overworld/Maps/PilarOW","BelowBullet")
PilarOwN = CreateSprite("Overworld/Maps/PilarOW Noche","BelowBullet")
PilarOw.SetParent(Corredor)
PilarOwN.SetParent(Corredor)
if Encounter["phase"] == 1 then
	Papaya = CreateSprite("Overworld/Characters/Papyrus/Bone","BelowBullet")
else
	Papaya = CreateSprite("Overworld/Characters/Papyrus/Normal","BelowBullet")
end
Papaya.SetPivot(0.5, 0)
Papaya.SetParent(Corredor)
Pilares = {CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top")}
Black = CreateSprite("Bullets/Bones/Body","Top")
Frisk.SetParent(Corredor)
for i=1,#Pilares do
	Pilares[i].SetParent(Corredor)
end
Black.color = {0,0,0}
BlackDefinitive.color = {0,0,0}
Black.Scale(640/5, 480)
BlackDefinitive.Scale(640/5, 480)
Frisk.MoveTo(221, -146)
Corredor.MoveTo(-100, -200)
Black.MoveTo(320, 240)
Black.alpha = 0
BlackDefinitive.MoveTo(320, 240)
BlackDefinitive.alpha = 0
PilarOw.MoveTo(100, 40)
PilarOwN.MoveTo(100, 40)
Papaya.MoveTo(469, -146)
Pilares[1].SetVar("Datos", -935)
Pilares[2].SetVar("Datos", -475)
Pilares[3].SetVar("Datos", -15)
Pilares[4].SetVar("Datos", 465)
Pilares[5].SetVar("Datos", 945)
Pilares[6].SetVar("Datos", 1347)
Pilares[7].SetVar("Datos", 1587)
Pilares[8].SetVar("Datos", 1827)
Pilares[9].SetVar("Datos", 2287)
Corredor.alpha = 0.5 + 0.5*math.sin(math.rad(180/12*(hour - 8)))
CorredorN.alpha = 0.5 + 0.5*math.sin(math.rad(180/12*(hour - 20)))
PilarOw.alpha = Corredor.alpha
PilarOwN.alpha = CorredorN.alpha
for i=1,#Pilares do
	Pilares[i].color = {0.6 + 0.4*math.sin(math.rad(180/12*(hour - 8))), 0.6 + 0.4*math.sin(math.rad(180/12*(hour - 8))), 0.6 + 0.4*math.sin(math.rad(180/12*(hour - 8)))}
end
	
timer = 0
tableaux = 0
Volumen = 1

function Update()
	FraskoX = Frisk.x
	FriskUpdate()
	DialogUpdate()
	if timer == 0 and Black.alpha < 1 then
		Black.alpha = Black.alpha + 0.04
		if Black.alpha >= 1 then
			if Encounter["Bone"] then
				Encounter["Bone"].Remove()
				Encounter["Bone"] = nil
				Encounter["BoneAnimation"] = nil
			end
			Corredor.MoveTo(-100, 280)
			BlackDefinitive.alpha = 1
			timer = 1
		end
	elseif timer > 0 and timer <= 60 then
		timer = timer + 1
	elseif timer == 61 and Black.alpha > 0 then
		Black.alpha = Black.alpha - 0.04
		if Black.alpha <= 0 then
			timer = 62
		end
	elseif timer > 61 and timer <= 121 then
		timer = timer + 1
	elseif timer == 122 then
		if Encounter["phase"] == 1 then
			Papaya2 = CreateSprite("Overworld/Characters/Papyrus/Normal","BelowBullet")
			Papaya2.SetPivot(0.5, 0)
			Papaya2.SetParent(Corredor)
			Papaya2.MoveTo(469, -146)
			Audio.PlaySound("spawn")
			timer = 123
		else
			timer = 124
		end
	elseif timer == 123 then
		Papaya.alpha = Papaya.alpha - 0.05
		if Papaya.alpha <= 0 then
			Papaya.Set("Overworld/Characters/Papyrus/Normal")
			Papaya.alpha = 1
			Papaya2.Remove()
			Papaya2 = nil
			timer = 124
		end
	elseif timer == 124 then
		if phase == 1 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Nyeh 2]Bueno[StopAnimation][SetCara:Nyeh 1][w:10][SetAnimation:Nyeh 2], vete ahora![StopAnimation][SetCara:Nyeh 1]", "papyrus", "Nyeh 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Nyeh 2]Well[StopAnimation][SetCara:Nyeh 1][w:10][SetAnimation:Nyeh 2], Off you go\rnow![StopAnimation][SetCara:Nyeh 1]", "papyrus", "Nyeh 1", "", 0, 400, nil, "", 1)
			end
		else
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Nyeh 2]Bueno[StopAnimation][SetCara:Nyeh 1][w:10][SetAnimation:Nyeh 2], mi trabajo\raquí está hecho![StopAnimation][SetCara:Nyeh 1]", "papyrus", "Nyeh 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Nyeh 2]Well[StopAnimation][SetCara:Nyeh 1][w:10][SetAnimation:Nyeh 2], my job here\ris done![StopAnimation][SetCara:Nyeh 1]", "papyrus", "Nyeh 1", "", 0, 400, nil, "", 1)
			end
		end
		timer = 125
	elseif timer == 125 and Boxes[1] and not Boxes[1].Activo then
		if phase == 1 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]Si me necesitas[StopAnimation][SetCara:Normal 1][w:10][SetAnimation:Normal 2],\restaré en mi casa\rhaciendo espagueti![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]If you need me[StopAnimation][SetCara:Normal 1][w:10][SetAnimation:Normal 2], I'll\rbe in my house\rcooking spaghetti![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			end
		else
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Worried 2]Si necesitas del\rapoyo de un gran\ramigo.[StopAnimation][SetCara:Worried 1]", "papyrus", "Worried 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Worried 2]If you need the\rsupport from a\rgreat friend.[StopAnimation][SetCara:Worried 1]", "papyrus", "Worried 1", "", 0, 400, nil, "", 1)
			end
		end
		timer = 126
	elseif timer == 126 and Boxes[1] and not Boxes[1].Activo then
		if phase == 1 then
			NewAudio.Pause("Loop")
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Thinking 2]...Estoy olvidando\ralgo?[StopAnimation][SetCara:Thinking 1]", "papyrus", "Thinking 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Thinking 2]...Am I forgetting\rsomething?[StopAnimation][SetCara:Thinking 1]", "papyrus", "Thinking 1", "", 0, 400, nil, "", 1)
			end
		else
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]Estaré en mi casa\rhaciendo espagueti![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]I'll be in my house\rcooking spaghetti![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			end
		end
		timer = 127
	elseif timer == 127 and Boxes[1] and not Boxes[1].Activo then
		if phase == 1 then
			NewAudio.Play("Loop")
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]Como sea![StopAnimation][SetCara:Normal 1][w:10][SetAnimation:Normal 2] No importa![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]Whatever![StopAnimation][SetCara:Normal 1][w:10][SetAnimation:Normal 2] It doesn't\rmatter![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			end
		else
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Nyeh 2]Espero tengas una\rbuena charla con\rel rey.[StopAnimation][SetCara:Nyeh 1]", "papyrus", "Nyeh 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Nyeh 2]I hope you have a\rgreat chat with\rthe king.[StopAnimation][SetCara:Nyeh 1]", "papyrus", "Nyeh 1", "", 0, 400, nil, "", 1)
			end
		end
		timer = 128
	elseif timer == 128 and Boxes[1] and not Boxes[1].Activo then
		if phase == 1 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:WTH 2]CHAU![StopAnimation][SetCara:WTH 1]", "papyrus", "WTH 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:WTH 2]TOODLES![StopAnimation][SetCara:WTH 1]", "papyrus", "WTH 1", "", 0, 400, nil, "", 1)
			end
		else
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]ME VOY AHORA![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus][SetAnimation:Normal 2]OFF I GO NOW![StopAnimation][SetCara:Normal 1]", "papyrus", "Normal 1", "", 0, 400, nil, "", 1)
			end
		end
		timer = 129
	elseif timer == 129 and Boxes[1] and not Boxes[1].Activo then
		Papaya.SetAnimation({"Overworld/Characters/Papyrus/Normal", "Overworld/Characters/Papyrus/Walking 1", "Overworld/Characters/Papyrus/Normal", "Overworld/Characters/Papyrus/Walking 2"}, 10/60)
		timer = 130
	elseif timer >= 130 and timer < 330 then
		timer = timer + 1
		Papaya.MoveTo(464 - 400/200*(timer - 130) + 5*math.cos(math.rad(360/20*(timer - 130))), -71 - 75*math.cos(math.rad(360/190*math.max(math.min(timer - 140, 190), 0))) - 5*math.sin(math.rad(360/20*(timer - 130))))
	elseif timer == 330 then
		Papaya.Remove()
		Papaya = nil
		timer = 331
	elseif timer >= 331 and timer < 421 then
		timer = timer + 1
		if timer <= 391 then
			Volumen = 1/60*(391 - timer)
			NewAudio.SetVolume("Loop", Volumen)
			if timer == 391 then
				NewAudio.Stop("Loop")
			end
		end
	elseif timer == 421 then
		Corredor.x = Corredor.x + 1
		if Corredor.x >= 98 then
			timer = 422
		end
	elseif timer > 421 and timer <= 540 then
		timer = timer + 1
	elseif timer == 541 then
		FriskAuto("Right")
		timer = 542
	elseif timer > 541 and timer <= 571 then
		timer = timer + 1
	elseif timer == 572 then
		Audio.PlaySound("suspenso")
		Black.alpha = 1
		timer = 573
	elseif timer > 572 and timer <= 752 then
		timer = timer + 1
	elseif timer == 753 then
		if Encounter["NOHIT"] and string.sub(GetAlMightyGlobal("Archivimentazo"), 17, 17) == "0" then
			SetAlMightyGlobal("Archivimentazo", string.sub(GetAlMightyGlobal("Archivimentazo"), 1, 16).."1"..string.sub(GetAlMightyGlobal("Archivimentazo"), 18))
			Encounter["LogroDesbloqueado"] = {Timer = 0, Icon = 17}
		end
		if Encounter["phase"] == 1 then
			Encounter["nextwaves"] = {"Cooking Spaghetti"}
		else
			Encounter["nextwaves"] = {"All that's left"}
		end
		for i=1,#Pilares do
			Pilares[i - tableaux].Remove()
			table.remove(Pilares, i - tableaux)
			tableaux = tableaux + 1
		end
		tableaux = 0
		Black.Remove()
		PilarOw.Remove()
		PilarOwN.Remove()
		Frisk.Remove()
		Corredor.Remove()
		CorredorN.Remove()
		BlackDefinitive.Remove()
		State("DEFENDING")
	end
	if timer >= 541 and timer < 573 then
		if Frisk.absx > 321 and Corredor.x > -760 then
			Corredor.x = Corredor.x - 3
		elseif Frisk.absx < 319 and Corredor.x < 1400 then
			Corredor.x = Corredor.x + 3
		end
	end
	for i=1,#Pilares do
		Pilares[i].x = Pilares[i].GetVar("Datos") + 1*(Corredor.x - 1400)
	end
end

function OnHit()
	
end